//
//  HJGroupNoticeController.m
//  KaiChat
//
//  Created by lx on 2023/11/7.
//

#import "HJGroupEditNoticeController.h"
#import "QMUITextView.h"
#import "HJIM.h"

@interface HJGroupEditNoticeController ()

@property (nonatomic, assign) NSInteger noticeMaxLenght;
@property (nonatomic, weak) UILabel *noticeLengthLabel;
@property (nonatomic, weak) QMUITextView *noticeTextView;
@property (nonatomic, weak) HJBarButton *doneButton;

@end

@implementation HJGroupEditNoticeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    self.navigationBar.title = @"Group notice";
    self.navigationBar.lineView.hidden = YES;
    
    _noticeMaxLenght = 600;
    
    HJBarButton *doneButton = [HJBarButton barButtonWithTitle:@"Done" color:UIColor.tj_mainColor target:self action:@selector(didClickDoneButton)];
    [doneButton setTitleColor:UIColor.tj_BEBDCAColor forState:UIControlStateDisabled];
    doneButton.enabled = NO;
    [doneButton sizeToFit];
    CGSize doneButtonSize = doneButton.tj_size;
    [self.navigationBar.contentView addSubview:doneButton];
    [doneButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.width.offset(doneButtonSize.width);
        make.height.offset(40);
        make.centerY.offset(0);
    }];
    _doneButton = doneButton;
    
    UIView *noticeView = [UIView addToView:self.view];
    noticeView.addCornerRadius(10);
    noticeView.backgroundColor = UIColor.tj_F6F6F6Color;
    [noticeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.inset(leftRightMargin);
        make.top.offset(20 + KNavBarHight);
        make.height.offset(160);
    }];
    
    
    QMUITextView *noticeTextView = [QMUITextView addToView:noticeView];
    noticeTextView.text = self.groupModel.notice;
    noticeTextView.font = UIFontMake(14);
    noticeTextView.tintColor = UIColor.tj_mainColor;
    noticeTextView.placeholderColor = UIColor.tj_placeholderTextColor;
    noticeTextView.placeholder = @"Enter the content you want to announce here";
    noticeTextView.backgroundColor = UIColor.tj_F6F6F6Color;
    noticeTextView.maximumTextLength = _noticeMaxLenght;
    [noticeTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(10);
        make.top.offset(8);
        make.bottom.inset(35);
        make.right.inset(10);
    }];
    
    UILabel *noticeLengthLabel = [UILabel lableWithSuperView:noticeView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    [noticeLengthLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(15);
        make.bottom.inset(0);
        make.height.offset(35);
    }];
    
    _noticeLengthLabel = noticeLengthLabel;
    _noticeTextView = noticeTextView;
        
    [self updataNoticeLengthLabel];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTextChanged:) name:UITextViewTextDidChangeNotification object:noticeTextView];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(500 * NSEC_PER_MSEC)), dispatch_get_main_queue(), ^{
        [self.noticeTextView becomeFirstResponder];
    });
    
}

- (void)handleTextChanged:(NSNotification *)not {
    if (not.object != _noticeTextView) return;
    
    self.doneButton.enabled = _noticeTextView.text.length;
    [self updataNoticeLengthLabel];
}

- (void)updataNoticeLengthLabel {
    if ( _noticeTextView.text.length > 0) {
        _noticeLengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _noticeTextView.text.length, _noticeMaxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _noticeTextView.text.length] rangeColor:UIColor.tj_333333Color color:UIColor.tj_BEBDCAColor];
    } else {
        _noticeLengthLabel.attributedText = [[NSString stringWithFormat:@"%ld/%ld", _noticeTextView.text.length, _noticeMaxLenght] attributedStringWithRangeString:[NSString stringWithFormat:@"%ld", _noticeTextView.text.length] rangeColor:UIColor.tj_BEBDCAColor color:UIColor.tj_BEBDCAColor];
    }
}

- (void)didClickDoneButton {
    
    [HJHUD showLoadingInView:self.view text:nil];
    
    [HJIM.shareIM.groupManager postGroupNoticeByGroupId:self.groupModel.groupId content:_noticeTextView.text completion:^(NSError * _Nullable error) {
        if (error) {
            [HJHUD showErrorMessageTip];
            return;
        };
        
        [HJHUD showSucceedMessageTip];

        self.groupModel.notice = self.noticeTextView.text;
        if (self.editComplete) {
            self.editComplete();
        }
        self.navigationController.viewControllers = [self removeNavChildViewControllers:@[@"HJGroupNoticeController"]];
        [[NSNotificationCenter defaultCenter] postNotificationName:publishGroupNotification object:self.groupModel];
        [self.navigationController popViewControllerToController:@"HJGroupChatController"];
    }];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:_noticeTextView];
}




@end
